<?php
namespace Club\Model;
use Think\Model;

class UserModel extends Model {
    protected $_auto = array(
        ["birthday", "strtotime", 3, "function"],
        array('create_time', 'time', 1, 'function'),
        array('update_time', 'time', 2, 'function'),
        array('password', 'passwordHash', 1, 'callback'), //新增用户时hash密码
        array('password', 'passwordHash', 2, 'callback'), //更新用户时hash密码
    );

    protected $_validate = array(
        array("id", "require", "ID必须填写!"),
        array("username", "require", "用户名必须填写!"),
        array("password", "require", "密码必须填写!", 0, "", 1),
        array("type", "require", "类型必须填写!"),
        array("create_time", "require", "创建时间必须填写!"),
        array("update_time", "require", "更新时间必须填写!"),
        array("repassword", "password", "重复密码不正确!", 0, "confirm"),
    );

    public $excel = false; //导入导出 默认false;true:导出&导出;false:不导入&不导出;import:只导入;export:只导出.

    public $delete = false; //list页面是否需要删除按钮,默认需要

    public $fieldMap = [
        'id' => [
            'type' => 'input', //字段类型,目前:input/password/select/ueditor/file/foreign/date/datetime
            'name' => 'ID', //显示名字
            'show' => true, //是否显示,默认是。true:所有终端显示;pc:只在pc显示，手机不显示;wap:只在手机显示，pc不显示
            'search' => true, //是否搜索,默认否
            'input' => false, //是否输入,默认是。true:update和create都输入;create:只有create输入,update readonly;update:只有update输入,create readonly.
            'required' => true, //是否必填,默认是
            'detail' => true, //默认是true; false为不在详情中显示
            'excel' => true, //默认是;true:导入导出;false:不导入不导出;import:仅导入;export:仅导出.
            'content' => '', //select/foreign的内容
            'list_specify' => '', //list中需要特殊显示的,注意需要<td></td>
        ],
        "username" => [
            "type" => "input",
            "name" => "用户名",
            "show" => true,
            "search" => true,
            "input" => 'create',
            "required" => true,
            "detail" => true,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "password" => [
            "type" => "password",
            "name" => "密码",
            "show" => false,
            "search" => false,
            "input" => true,
            "required" => true,
            "detail" => false,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
            "update_specify" => '
            <div class="form-group">
                <label class="col-xs-12 col-sm-3 col-md-3 control-label no-padding-right" for="password"> 密码 </label>
                <div class="col-xs-12 col-sm-6">
                    <span class="block input-icon input-icon-right">
                        <input type="password" class="width-100" placeholder="密码" id="password" name="password"  value="" validate-required="required:请填写密码" />
                        <i class=""></i>
                    </span>
                </div>
                <div class="help-block col-xs-12 col-sm-reset inline" style="margin-bottom: 5px;"></div>
             </div>
            ',
        ],
        "type" => [
            "type" => "select",
            "name" => "类型",
            "show" => true,
            "search" => true,
            "input" => false,
            "required" => true,
            "detail" => true,
            "excel" => true,
            "content" => "用户,商家,导游,旅社,管理员",
            "list_specify" => "",
        ],
        "qq_key" => [
            "type" => "input",
            "name" => "qq唯一编码",
            "show" => false,
            "search" => false,
            "input" => false,
            "required" => false,
            "detail" => false,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "wechat_key" => [
            "type" => "input",
            "name" => "微信唯一编码",
            "show" => false,
            "search" => false,
            "input" => false,
            "required" => false,
            "detail" => false,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "weibo_key" => [
            "type" => "input",
            "name" => "微博唯一编码",
            "show" => false,
            "search" => false,
            "input" => false,
            "required" => false,
            "detail" => false,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "name" => [
            "type" => "input",
            "name" => "姓名",
            "show" => true,
            "search" => true,
            "input" => true,
            "required" => true,
            "detail" => true,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "photo" => [
            "type" => "file",
            "name" => "头像",
            "show" => true,
            "search" => false,
            "input" => true,
            "required" => false,
            "detail" => true,
            "excel" => true,
            "content" => "",
            "list_specify" => '<td class="center"><img src="__PUBLIC__/file/{$vo[photo]}" width="50" height="50"/></td>',
        ],
        "location" => [
            "type" => "input",
            "name" => "所在地",
            "show" => true,
            "search" => false,
            "input" => true,
            "required" => false,
            "detail" => true,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "id_number" => [
            "type" => "input",
            "name" => "身份证号",
            "show" => false,
            "search" => false,
            "input" => true,
            "required" => false,
            "detail" => true,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "birthday" => [
            "type" => "date",
            "name" => "生日",
            "show" => false,
            "search" => false,
            "input" => true,
            "required" => false,
            "detail" => true,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "email" => [
            "type" => "input",
            "name" => "邮箱",
            "show" => false,
            "search" => false,
            "input" => true,
            "required" => false,
            "detail" => true,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "balance" => [
            "type" => "input",
            "name" => "余额",
            "show" => true,
            "search" => false,
            "input" => false,
            "required" => false,
            "detail" => true,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "bank" => [
            "type" => "input",
            "name" => "银行",
            "show" => false,
            "search" => false,
            "input" => true,
            "required" => false,
            "detail" => true,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "card_no" => [
            "type" => "input",
            "name" => "银行卡号",
            "show" => false,
            "search" => false,
            "input" => true,
            "required" => false,
            "detail" => true,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "state" => [
            "type" => "select",
            "name" => "状态",
            "show" => true,
            "search" => false,
            "input" => false,
            "required" => true,
            "detail" => true,
            "excel" => true,
            "content" => "正常,禁用",
            "list_specify" => "",
        ],
        "create_time" => [
            "type" => "datetime",
            "name" => "注册时间",
            "show" => true,
            "search" => false,
            "input" => false,
            "required" => true,
            "detail" => true,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
        "update_time" => [
            "type" => "datetime",
            "name" => "更新时间",
            "show" => false,
            "search" => false,
            "input" => false,
            "required" => true,
            "detail" => true,
            "excel" => true,
            "content" => "",
            "list_specify" => "",
        ],
    ];

    protected function passwordHash($data = '') {
        if ($data != '') {
            return password_hash($data, PASSWORD_BCRYPT);
        } else {
            return $this->find($this->id)['password'];
        }
    }

    public function login($type = 'user') {
        //验证账号
        $query = "type='".$type."' AND username='".$this->username."'";
        $user = M('User')->where($query)->find();
        if (!$user) {
            $this->error = '用户名不存在!';
            return false;
        }
        //验证密码
        if (!password_verify($this->password, $user['password'])) {
            $this->error = '密码错误!';
            return false;
        }
        //验证用户类型
        if ($user['type'] != $type) {
            $this->error ='请求不合法!';
            return false;
        }
        //验证用户状态
        if ($user['state'] != '正常') {
            $this->error = '用户被禁用!';
            return false;
        }

        //登录成功
        session($type, $user);
        return true;
    }

}
